package com.zline.foodraise.http;

import android.util.SparseArray;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.entity.CommentBean;
import com.zline.foodraise.entity.CustomerUserBean;
import com.zline.foodraise.entity.H5Params;
import com.zline.foodraise.entity.MomentBean;
import com.zline.foodraise.entity.MsgBean;
import com.zline.foodraise.entity.NoticeBean;
import com.zline.foodraise.entity.CfOrderBean;
import com.zline.foodraise.entity.OrderRedPointBean;
import com.zline.foodraise.entity.ProductBean;
import com.zline.foodraise.entity.ProjectBean;
import com.zline.foodraise.entity.MsgNoticeRedPointBean;
import com.zline.foodraise.entity.ShopApplyBean;
import com.zline.foodraise.entity.TagBean;
import com.zline.foodraise.entity.TemplateBean;
import com.zline.foodraise.entity.TradingBean;
import com.zline.foodraise.entity.UserAddressBean;
import com.zline.foodraise.entity.UserBean;
import com.zline.foodraise.entity.WxPayInfo;
import com.zline.foodraise.util.LUtil;

import org.androidannotations.annotations.EBean;
import org.json.JSONObject;

import java.util.Iterator;

/**
 * Created by heshantao on 15/7/21.
 * Http 数据解析
 */
@EBean
public class HttpDataParsing {
    private static final String LOG_TAG = HttpDataParsing.class.getSimpleName();

    //解析关注、粉丝数据
    public SparseArray<CustomerUserBean> getAttentionFansUserList(JSONObject json) {
        SparseArray<CustomerUserBean> sparseArray = new SparseArray<CustomerUserBean>();
        String attentionFansInfo = json.toString();
        CustomerUserBean attentionFansUser;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(attentionFansInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.USER_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______是jsonArray________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                attentionFansUser = gson.fromJson(obj, CustomerUserBean.class);
                LUtil.i(LOG_TAG, "_______昵称________" + attentionFansUser.getNickName()
                        + "_________图片_______" + attentionFansUser.getAvatarPicUrl());
                sparseArray.put(i, attentionFansUser);
                i++;
            }
        }
        return sparseArray;
    }


    //解析用户商品列表
    public SparseArray<ProductBean> getUserProductList(JSONObject json) {
        SparseArray<ProductBean> sparseArray = new SparseArray<ProductBean>();
        String userProductInfo = json.toString();
        ProductBean userProductBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(userProductInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.PRODUCT_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______getUserProductList________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                userProductBean = gson.fromJson(obj, ProductBean.class);
                sparseArray.put(i, userProductBean);
                i++;
            }
        }
        return sparseArray;
    }


    //解析项目列表
    public SparseArray<ProjectBean> getUserProjectList(JSONObject json) {
        SparseArray<ProjectBean> sparseArray = new SparseArray<ProjectBean>();
        String projectInfo = json.toString();
        ProjectBean projectBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(projectInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.PROJECT_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______getUserProjectList________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                projectBean = gson.fromJson(obj, ProjectBean.class);
                sparseArray.put(i, projectBean);
                //LUtil.i(LOG_TAG, "_______项目名称________" + projectBean.getProjectName());
                i++;
            }
        }
        return sparseArray;
    }


    //解析获得用户基本信息
    public UserBean getUserBean(JSONObject json) {
        String userInfo = json.toString();
        JsonElement jsonElement;
        JsonObject jsonObject;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(userInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.USER_BEAN);
        jsonObject = jsonElement.getAsJsonObject();
        UserBean userBean = gson.fromJson(jsonObject, UserBean.class);
        return userBean;
    }


    //解析店铺申请状态
    public ShopApplyBean getShopApplyBean(JSONObject json) {
        String userInfo = json.toString();
        JsonElement jsonElement;
        JsonObject jsonObject;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(userInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.SHOP_APPLY_BEAN);
        jsonObject = jsonElement.getAsJsonObject();
        ShopApplyBean shopApplyBean = gson.fromJson(jsonObject, ShopApplyBean.class);
        return shopApplyBean;
    }

    //解析标签列表
    public SparseArray<TagBean> getTagList(JSONObject json) {
        SparseArray<TagBean> sparseArray = new SparseArray<TagBean>();
        String tagInfo = json.toString();
        TagBean tagBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(tagInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.TAG_BEAN_LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______getTagList________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                tagBean = gson.fromJson(obj, TagBean.class);
                sparseArray.put(i, tagBean);
                LUtil.i(LOG_TAG, "_______标签名________" + tagBean.getContent());
                i++;
            }
        }
        return sparseArray;
    }


    //解析众筹项目订单
    public SparseArray<CfOrderBean> getProjectOrderList(JSONObject json) {
        SparseArray<CfOrderBean> sparseArray = new SparseArray<CfOrderBean>();
        String orderInfo = json.toString();
        CfOrderBean orderBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(orderInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.CF_ORDER_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______getProjectOrderList________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                orderBean = gson.fromJson(obj, CfOrderBean.class);
                sparseArray.put(i, orderBean);
                i++;
            }
        }
        return sparseArray;
    }


    //解析动态列表
    public SparseArray<MomentBean> getDynamicList(JSONObject json) {
        SparseArray<MomentBean> sparseArray = new SparseArray<MomentBean>();
        String dynamicInfo = json.toString();
        MomentBean momentBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(dynamicInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.MOMENT_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______getDynamicList________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                momentBean = gson.fromJson(obj, MomentBean.class);
                sparseArray.put(i, momentBean);
                i++;
            }
        }
        return sparseArray;
    }


    //解析赞列表
    public SparseArray<CustomerUserBean> getSupportList(JSONObject json) {
        SparseArray<CustomerUserBean> sparseArray = new SparseArray<CustomerUserBean>();
        String supportInfo = json.toString();
        CustomerUserBean supportUser;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(supportInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.USER_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______是jsonArray________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                supportUser = gson.fromJson(obj, CustomerUserBean.class);
                sparseArray.put(i, supportUser);
                i++;
            }
        }
        return sparseArray;
    }

    //解析评论列表
    public SparseArray<CommentBean> getCommentList(JSONObject json) {
        SparseArray<CommentBean> sparseArray = new SparseArray<CommentBean>();
        String commentInfo = json.toString();
        CommentBean commentBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(commentInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.COMMENT_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______是jsonArray________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                commentBean = gson.fromJson(obj, CommentBean.class);
                sparseArray.put(i, commentBean);
                i++;
            }
        }
        return sparseArray;
    }


    //解析地址列表
    public SparseArray<UserAddressBean> getUserAddrList(JSONObject json) {
        SparseArray<UserAddressBean> sparseArray = new SparseArray<UserAddressBean>();
        String addrInfo = json.toString();
        UserAddressBean userAddressBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(addrInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.USER_ADDRESS_BEAN_LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______是jsonArray________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                userAddressBean = gson.fromJson(obj, UserAddressBean.class);
                sparseArray.put(i, userAddressBean);
                i++;
            }
        }
        return sparseArray;
    }


    //解析众筹关注列表
    public SparseArray<CustomerUserBean> getProjectAttentionList(JSONObject json) {
        SparseArray<CustomerUserBean> sparseArray = new SparseArray<CustomerUserBean>();
        String supportInfo = json.toString();
        CustomerUserBean supportUser;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(supportInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.USER_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            LUtil.i(LOG_TAG, "_______是jsonArray________");
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                supportUser = gson.fromJson(obj, CustomerUserBean.class);
                sparseArray.put(i, supportUser);
                i++;
            }
        }
        return sparseArray;
    }

    //从服务器解析微信支付相关信息
    public WxPayInfo getWxPayInfo(JSONObject object) {
        JsonElement jsonElement;
        JsonObject jsonObject;
        String payInfo;
        payInfo = object.toString();
        jsonElement = new JsonParser().parse(payInfo);
        jsonObject = jsonElement.getAsJsonObject();
        payInfo=jsonObject.get(Constant.PAY_INFO).getAsString();
        jsonElement = new JsonParser().parse(payInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement=jsonObject.get(Constant.PAY_STRING);
        jsonObject = jsonElement.getAsJsonObject();
        LUtil.i(LOG_TAG,"______payInfo_____"+jsonObject.toString());
        String appId=jsonObject.get(Constant.APP_ID).getAsString();
        String packageWx=jsonObject.get(Constant.PACKAGE_WX).getAsString();
        String timeStamp=jsonObject.get(Constant.TIME_STAMP).getAsString();
        String sign=jsonObject.get(Constant.SIGN).getAsString();
        String nonceStr=jsonObject.get(Constant.NONCESTR).getAsString();
        String prepayId=jsonObject.get(Constant.PREPAY_ID).getAsString();
        String partnerid=jsonObject.get(Constant.PARTNER_ID).getAsString();
        WxPayInfo wxPayInfo=new WxPayInfo(appId,packageWx,nonceStr,partnerid,prepayId,sign,timeStamp);
        return wxPayInfo;
    }


    //从服务器解析微信支付相关信息
    public String getAliPayInfo(JSONObject object) {
        JsonElement jsonElement;
        JsonObject jsonObject;
        String payInfo;
        payInfo = object.toString();
        jsonElement = new JsonParser().parse(payInfo);
        jsonObject = jsonElement.getAsJsonObject();
        payInfo=jsonObject.get(Constant.PAY_INFO).getAsString();
        jsonElement = new JsonParser().parse(payInfo);
        jsonObject = jsonElement.getAsJsonObject();
        payInfo=jsonObject.get(Constant.PAY_STRING).getAsString();
        LUtil.i(LOG_TAG,"______payInfo_____"+payInfo);
        return payInfo;
    }

    //从服务器解析模板相关信息
    public SparseArray<TemplateBean> getTemplateInfo(JSONObject json) {
        SparseArray<TemplateBean> sparseArray = new SparseArray<TemplateBean>();
        String templateInfo = json.toString();
        TemplateBean templateBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(templateInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.TEMPLATE_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                templateBean = gson.fromJson(obj, TemplateBean.class);
                LUtil.i(LOG_TAG, "_______是jsonArray________"+templateBean.getAndroidImgUrl());
                sparseArray.put(i, templateBean);
                i++;
            }
        }
        return sparseArray;
    }


    //从服务器解析红包交易明细相关信息
    public SparseArray<TradingBean> getTradingInfo(JSONObject json) {
        SparseArray<TradingBean> sparseArray = new SparseArray<TradingBean>();
        String tradingInfo = json.toString();
        TradingBean tradingBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(tradingInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.USER_ACCOUNT_DETAIL_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                tradingBean = gson.fromJson(obj, TradingBean.class);
                LUtil.i(LOG_TAG, "_______交易时间________"+tradingBean.getCreateTimeStamp());
                sparseArray.put(i, tradingBean);
                i++;
            }
        }
        return sparseArray;
    }


    //从服务器解析消息中心消息相关信息
    public SparseArray<MsgBean> getMsgCentreMsgInfo(JSONObject json) {
        SparseArray<MsgBean> sparseArray = new SparseArray<MsgBean>();
        String msgInfo = json.toString();
        MsgBean msgBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(msgInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.SYS_PUSH_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                msgBean = gson.fromJson(obj, MsgBean.class);
                LUtil.i(LOG_TAG, "_______消息信息________"+msgBean.getContent());
                sparseArray.put(i, msgBean);
                i++;
            }
        }
        return sparseArray;
    }

    //从服务器解析活动公告相关信息
    public SparseArray<NoticeBean> getEventNoticeInfo(JSONObject json) {
        SparseArray<NoticeBean> sparseArray = new SparseArray<NoticeBean>();
        String noticeInfo = json.toString();
        NoticeBean noticeBean;
        JsonElement jsonElement;
        JsonObject jsonObject;
        int i = 0;
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(noticeInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.NOTICE_BEAN_PAGE);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement = jsonObject.get(Constant.LIST);
        if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = jsonElement.getAsJsonArray();//转为数组
            for (Iterator iter = jsonArray.iterator(); iter.hasNext(); ) {
                JsonObject obj = (JsonObject) iter.next();
                noticeBean = gson.fromJson(obj, NoticeBean.class);
                LUtil.i(LOG_TAG, "_______活动公告________"+noticeBean.getTitle());
                sparseArray.put(i, noticeBean);
                i++;
            }
        }
        return sparseArray;
    }


    //从服务器解析公告信息
    public NoticeBean getNoticeInfo(JSONObject object) {
        JsonElement jsonElement;
        JsonObject jsonObject;
        NoticeBean noticeBean;
        String noticeInfo = object.toString();
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(noticeInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement=jsonObject.get(Constant.NOTICE_BEAN);
        jsonObject = jsonElement.getAsJsonObject();
        noticeBean=gson.fromJson(jsonObject, NoticeBean.class);
        return noticeBean;
    }



    //从服务器解析消息活动公告小红点信息
    public MsgNoticeRedPointBean getRedPointInfo(JSONObject object) {
        JsonElement jsonElement;
        JsonObject jsonObject;
        MsgNoticeRedPointBean bean;
        String noticeInfo = object.toString();
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(noticeInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement=jsonObject.get(Constant.RED_POINT_BEAN);
        jsonObject = jsonElement.getAsJsonObject();
        bean=gson.fromJson(jsonObject, MsgNoticeRedPointBean.class);
        return bean;
    }



    //从服务器解析订单小红点信息
    public OrderRedPointBean getOrderRedPointInfo(JSONObject object) {
        JsonElement jsonElement;
        JsonObject jsonObject;
        OrderRedPointBean bean;
        String noticeInfo = object.toString();
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(noticeInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement=jsonObject.get(Constant.ORDER_RED_POINT_BEAN);
        jsonObject = jsonElement.getAsJsonObject();
        bean=gson.fromJson(jsonObject, OrderRedPointBean.class);
        return bean;
    }


    //从服务器解析web初始根目录
    public H5Params getH5InitInfo(JSONObject object) {
        JsonElement jsonElement;
        JsonObject jsonObject;
        H5Params bean;
        String noticeInfo = object.toString();
        Gson gson = new Gson();
        jsonElement = new JsonParser().parse(noticeInfo);
        jsonObject = jsonElement.getAsJsonObject();
        jsonElement=jsonObject.get(Constant.PARAMS);
        jsonObject = jsonElement.getAsJsonObject();
        bean=gson.fromJson(jsonObject, H5Params.class);
        return bean;
    }

}
